<?php
	$_SESSION['TenDichVu']= "";
	$_SESSION['NoiDungThuGon']= "";

	$_SESSION['NoiDungDichVu']= "";

	$_SESSION['Tags']= "";

	$_SESSION['Mota']= "";

	include_once("../../coms/xl_trang.php");

	include_once("../../include/ckeditor/ckeditor.php");

	include_once("../../include/convert_link.php");

	if(!isset($_REQUEST['key'])):

		header("location:trangchu.php");

	else:

		if(!isset($_REQUEST['madm'])):

			header("location:trangchu.php");

		else:

			$MaDanhMuc = $_REQUEST['madm'];		

		

?>



<script type="text/javascript">

function show_error(id, text){

	

	$("#validateError").html(text);	

	$("#validateError").addClass( "validateTips" );

	id.focus();

	setTimeout(function() {		

		$("#validateError").removeClass( "validateTips");

		$("#validateError").html("");

	}, 3500 );

}



function Kiemtra_Them_Dich_Vu(form1)

{

	var myfrmObj=document.createElement("form");

	myfrmObj=form1;	

	

	if($('input[name*="txt_TieuDe"]').val()=="")

	{		

		show_error($('input[name*="txt_TieuDe"]'), "Tên dịch vụ không được để trống.Vui lòng nhập lại!!!");		

		return false;

	}

}

</script>



<form action="trangchu.php?key=t_qldv&madm=<?php echo $MaDanhMuc;?>" method="post" name="frm_ins_dich_vu"  onsubmit="return Kiemtra_Them_Dich_Vu(this)" enctype="multipart/form-data">



<div style="width:670px; float:left; padding-left:5px;">

<?php 

				

		$CKEditor_new = new CKEditor();

		$CKEditor_new->basePath = '../../include/ckeditor/';

		$CKEditor_new->config['height'] = 500;

		$CKEditor_new->config['width'] =680;

		

		$CKEditor_Title = new CKEditor();

		$CKEditor_Title->basePath = '../../include/ckeditor/';

		$CKEditor_Title->config['height'] = 80;

		$CKEditor_Title->config['width'] = 680;

		

		/*Button cap nhat*/

		if(isset($_POST['btn_HoanTat'])):

			$tendichvu = $_POST['txt_TieuDe'];

			$tendichvu = trim(strtolower($tendichvu));

			

			$conv = new convert_link();

			$tendichvu_convert = $conv->convert_vi_to_en($tendichvu);

			$kt = new xl_trang();

			$kt_tontai = $kt->kt_ten_viet_tat($tendichvu_convert);

			if($kt_tontai):

?>

			<script type="text/javascript">

                alert("Tên dịch vụ đã tồn tại. Vui lòng nhập lại!!!");		

            </script>

<?php

			else:

			

				$_SESSION['TenDichVu']= $_POST['txt_TieuDe'];

				$_SESSION['NoiDungDichVu']= $_POST['noidung_daydu'];

				$_SESSION['Tags']= $_POST['tags'];

				$MaDanhMuc = $_REQUEST['madm'];

				

				

				$noidung = $_POST['noidung_daydu'];

				$tags = $_POST['tags'];

				//Toan - ADD - START - 08/03/2012

				$_SESSION['Mota'] = $_POST['txtmota'];

				$mota = $_POST['txtmota'];

				//Toan - ADD - END - 08/03/2012

				$timezone  = +7; //(GMT +7:00) 

				$thoigiantaomoi = gmdate("Y-m-d H:i:s", time() + 3600*($timezone+date("0"))); 

				$manguoidung = $_SESSION['MaNguoiDung'];

				$noidungthugon = $_POST['noidung_thugon'];	

				$file = $_FILES['up_hinh']['name'];

				$kq=array(0);

				$them = new xl_trang();

				if($file != NULL)

				{

					$kq=up_hinh();

			

					if($kq[2]==true)

					{

						$tenhinhanh = "images/dichvu/".$kq[1];

						$ketqua = $them->them_dich_vu($MaDanhMuc,$_POST['txt_TieuDe'], $noidung,$noidungthugon, $tags, $thoigiantaomoi, $manguoidung, $tendichvu_convert, $mota, $tenhinhanh);

						echo '<script type="text/javascript">

								alert("Thêm dịch vụ thành công!!!");				

							</script>';

						echo '<meta http-equiv="refresh" content="0;url=trangchu.php?key=qldv&madm='.$MaDanhMuc.'"  />';

					}

				}

				else

				{

					$ketqua = $them->them_dich_vu_khong_hinh_anh($MaDanhMuc,$_POST['txt_TieuDe'], $noidungthugon,$noidung, $tags, $thoigiantaomoi, $manguoidung, $tendichvu_convert, $mota);

					echo '<script type="text/javascript">

								alert("Thêm dịch vụ thành công!!!");				

							</script>';

					echo '<meta http-equiv="refresh" content="0;url=trangchu.php?key=qldv&madm='.$MaDanhMuc.'"  />';

				}	

	

						

						

			endif;			

		endif;

		

		/*Button Kiem tra ton tai*/

		if(isset($_REQUEST['btn_KiemTraTonTai'])):

			$tendichvu = $_POST['txt_TieuDe'];

			$tendichvu = trim(strtolower($tendichvu));

			$conv = new convert_link();

			$tendichvu_convert = $conv->convert_vi_to_en($tendichvu);

			$kt = new xl_trang();

			$kt_tontai = $kt->kt_ten_viet_tat($tendichvu_convert);

			if($kt_tontai):

?>

			<script type="text/javascript">

                alert("Tên dịch vụ đã tồn tại. Vui lòng nhập lại!!!");		

            </script>

<?php

			else:

				$_SESSION['TenDichVu']= $_POST['txt_TieuDe'];				

?>

			<script type="text/javascript">

                alert("Bạn có thể dùng tên này!!!");		

            </script>

<?php				

			endif;	

		endif;

		



	

?>	

<div class="content_right_title_admin">
Thêm dịch vụ 

</div> 

<div class="content_right_group_admin box_shadow" style="padding-bottom:5px">
     <div style="display:block; text-align:right">

        <div style="padding-top:10px; padding-bottom:10px; padding-left:10px" >
			<input type="submit" name="btn_HoanTat" value="Hoàn tất" class="button medium blue">
			<a href="trangchu.php?key=alb" class="button medium blue">Thư viện hình ảnh</a>
         
             <input type="button" value="Quay lại" onclick="goBack()" class="button medium blue">
             

        </div>

    </div>
    

    <strong>- Tên dịch vụ:&nbsp;&nbsp;</strong>

    <input type="text" name="txt_TieuDe" style="width:570px; padding-bottom:5px" class="border_radius" value="<?php 

    if(isset($_SESSION['TenDichVu'])) echo $_SESSION['TenDichVu'];?>"/><br />

     <div id="idvalidateTips" align="center" style="width:auto; padding-bottom:5px;">

     <input type="submit" name="btn_KiemTraTonTai" value="Kiểm tra tồn tại" class="button medium blue"><br />

     <span id="validateError"  style="color:#FF0000"></span></div>

    <br />

    <strong>- Nội dung thu gọn:<br /></strong>

    <?php $CKEditor_Title->editor("noidung_thugon",$_SESSION['NoiDungThuGon']);?><br />

    <strong>- Nội dung đầy đủ:<br /></strong>

    <?php $CKEditor_new->editor("noidung_daydu",$_SESSION['NoiDungDichVu']);?><br />

    <strong>- Hình đại diện:<br /></strong>

    <input type="file" name="up_hinh" class="border_radius"/><br />

    <strong>- Tags:<br /></strong>

    <?php 

        //Phuong - MOD - START - 06032012

        echo '<textarea name="tags" style="width:670px; height:100px" class="border_radius">'.$_SESSION['Tags'].'</textarea>';	

        //Phuong - MOD - END - 06032012	

    ?><br /><br />

    

    <strong>- Mô tả:<br /></strong>

    <?php 

        //TOAN - ADD - START - 08032012

        echo '<textarea name="txtmota" style="width:670px; height:100px" class="border_radius">'.$_SESSION['Mota'].'</textarea>';	

        //TOAN - ADD - END - 08032012	

    ?>

    <div style="display:block; text-align:center">

    	 <input type="submit" name="btn_HoanTat" value="Hoàn tất" class="button medium blue">

         <input type="button" name="btn_ThemHinhAnh" value="Thư viện hình ảnh" class="button medium blue">

    </div>

    

    </div>

</div>

<?php 

	endif;

endif;

function up_hinh()

{

	$result = "";

	define('UPLOAD_DIR','../../images/dichvu/');

	define('MAX_FILES_SIZE',10000000);

	$file = str_replace(" ","_",$_FILES['up_hinh']['name']);

	//convert maximun size to KB

	$max = number_format(MAX_FILES_SIZE/1024,1)." KB";

	//create an array of permitted MIME types

	$premitted = array('image/gif','image/jpeg','image/pjpeg','image/png');

	//begin by assuming the file is unacceptable

	$sizeOK = false;

	$typeOK = false;

	$success = false;

	$kq=array(3);

	//Check that file is within the permitted size

	if($_FILES['up_hinh']['size']>0 && $_FILES['up_hinh']['size']<= MAX_FILES_SIZE)

	{

		$sizeOK = true;

	}

	

	//Check that file is within the permitted size

	foreach($premitted as $type)

	{

		if($type == $_FILES['up_hinh']['type'])

		{

			$typeOK = true;

			break;

		}

	}

	

	if($sizeOK && $typeOK)

	{

		switch($_FILES['up_hinh']['error'])

		{

			case 0:

				if(!file_exists(UPLOAD_DIR.$file))

				{			

					$success = move_uploaded_file($_FILES['up_hinh']['tmp_name'],UPLOAD_DIR.$file);

					$kq[1]=$file;

					$kq[0] = UPLOAD_DIR;

					

				}

				else

				{

					$now  = date("YmdHis",time());

					$file = $now.$file;

					$success = move_uploaded_file($_FILES['up_hinh']['tmp_name'],UPLOAD_DIR.$file);

					$kq[1]=$file;

					$kq[0] = UPLOAD_DIR;

				}

				if($success){

					$result = "$file upload thành công";

					//$result = "$file uploaded successfully";

				}

				else{

					$result = "Lỗi upload $file.Vui long thử lại.";

					//$result = "Error uploading $file.Please try again.";

				}

				break;

			case 3:

				$result = "Lỗi upload $file.Vui long thử lại.";

				//$result = "Error uploading $file.Please try again.";

			default:

				$result = "Lỗi hệ thống upload $file.Vui long thử lại.";

				//$result = "System error uploadding $file contact webmaster";

		}

	}

	elseif($_FILES['up_hinh']['error']==4){

		$result = "Chưa chọn file upload";

		//$result = "No file selected";

	}

	else{

		$result = "Lỗi file upload. Maximum size: $max. File upload phải là gif, jpg, png.";

		//$result ="$file cannot be uploaded. Maximum size: $max. Acceptable file types: gif, jpg, png.";

	}

	if($success == TRUE)

	{

		echo '<script language="javascript">

				window.alert("'.$result.'");

			</script>';

		//echo '<meta http-equiv="refresh" content="2;url=them_tintuc.php"  />';

	}

	else

	{

		echo '<script language="javascript">

				window.alert("'.$result.'");

			</script>';

		//echo '<meta http-equiv="refresh" content="2;url=them_tintuc.php"  />';

	}

	$kq[2] = $success;

	return $kq;

}	



 ?>

</form>